<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="所属公司">
        <el-select v-model="formInline.gongsiSel" @change="initData" placeholder="所属公司">
          <el-option v-for="val in gongsiList" :key="val.id" :label="val.name" :value="val.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="formInline.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品类别">
        <el-cascader :options="orderList" @change="initData" :props="{value:'id',label:'name'}" v-model="formInline.categoryId"></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="check">查询</el-button>
      </el-form-item>
      <el-button icon="el-icon-refresh" @click="handleReset">
        重置
      </el-button>
    </el-form>
    <el-button type="primary" @click="confirmDele">
      删除商品
    </el-button>

    <el-table :data="dataList" style="width: 100%;margin-top:30px;" @selection-change="handleSelectionChange" border>
      <el-table-column
        type="selection">
      </el-table-column>
      <el-table-column align="center" prop="name" label="商品名称" width="220">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="商品条码" width="220">
        <template slot-scope="scope">
          {{ scope.row.barCode }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="商品大类">
        <template slot-scope="scope">
          快消品
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="商品类别">
        <template slot-scope="scope">
          {{ scope.row.topCategoryName }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="商品子类">
        <template slot-scope="scope">
          {{ scope.row.secondCategoryName }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="商品图片">
        <template slot-scope="scope">
          <img style="width: 40px;" :src="scope.row.img" alt="">
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="成本">
        <template slot-scope="scope">
          {{ scope.row.cost }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="售价">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="折扣">
        <template slot-scope="scope">
          {{ scope.row.discount }}
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="160">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<el-dialog :visible.sync="dialogVisible" width="400px" :title="dialogType==='edit'?'编辑商品':'新增商品'">
      <el-form :model="role" :rules="rules" label-width="120px" label-position="left">
        <el-form-item label="成本（元）" prop="name">
          <el-input v-model="role.normal.cost" placeholder="成本" />
        </el-form-item>
        <el-form-item label="售价（元）" width="80px" prop="name">
          <el-input v-model="role.normal.price" placeholder="售价" />
        </el-form-item>
        <el-form-item label="折扣（元）" width="80px" prop="name">
          <el-input v-model="role.normal.discount" placeholder="折扣" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">
          取消
        </el-button>
        <el-button type="primary" @click="confirmRole">
          确定
        </el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFillBuckle" :title="productType=='del'?'删除商品':'加入公司商品库'">
      <el-table :data="multipleSelection" style="width: 100%;margin-top:30px;" border>
        <el-table-column align="center" prop="name" label="名称" width="220">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="类别" width="220">
          <template slot-scope="scope">
            {{ scope.row.barcode }}
          </template>
        </el-table-column>
        <el-table-column align="header-center" label="状态">
          <template slot-scope="scope">
            {{ scope.row.barcode }}
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align:right;margin-top: 40px;">
        <el-button type="danger" @click="dialogFillBuckle=false">
          取消
        </el-button>
        <el-button type="primary" @click="confirmDele">
          确定
        </el-button>
      </div>
    </el-dialog>
    <Pagination :total="total" :page="page+1" @pagination="pagination" />
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { getRoutes, getRoles, addRole, deleteRole, updateRole } from '@/api/role'
import { getTenant, putTenant, listTenant, offsaleMaintain, onsaleMaintain } from '@/api/TenantGoods'
import Pagination from '@/components/Pagination/index'
import { category } from '@/api/user'

const defaultRole = {
  "type": 0,
  "normal": {
    "name": "",
    "topCategoryId": "",
    "secondCategoryId": "",
    "img": "",
    "price": 0,
    "cost": 0,
    "discount": 0
  }
}
export default {
  data() {
    return {
      gongsiList:[],
      productType: 'join',
      role: Object.assign({}, defaultRole),
      routes: [],
      dataList: [],
      dialogVisible: false,
      dialogFillBuckle: false,
      dialogRefund: false,
      dialogType: 'new',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      rules: {
        normal:{
          price: [
            { required: true, message: '请输入成本' },
          ],
          cost: [
            { required: true, message: '请输入售价' },
          ],
          discount: [
            { required: true, message: '请输入折扣' },
          ],
        }
      },
      options1: [{
        value: '选项1',
        label: '测试设备'
      }, {
        value: '选项2',
        label: '预投放设备'
      }, {
        value: '选项3',
        label: '运营设备'
      }, {
        value: '选项4',
        label: '停用设备'
      }],
      options2: [{
        value: '选项1',
        label: '已采购'
      }, {
        value: '选项2',
        label: '已注册'
      }, {
        value: '选项3',
        label: '已激活'
      }, {
        value: '选项4',
        label: '正常'
      }, {
        value: '选项5',
        label: '维护中'
      }, {
        value: '选项6',
        label: '异常设备'
      }],
      orderList: [],
      total: 0,
      page: 0,
      formInline: {
        name: '',
        categoryId: '',
        gongsiSel: ''
      },
      goodId:'',
      multipleSelection:[],
    }
  },
  computed: {
  },
  components: { Pagination },
  created() {
    this.gongsiList = JSON.parse(localStorage.getItem('companyList'));
    this.formInline.gongsiSel = this.gongsiList[0].id;
    this.initData();
    this.orderListData();
  },
  methods: {
    //列表
    initData(){
      var data = {
        searchCriteria: {
          name: this.formInline.name,
          barCode: "",
          type: 0,
          categoryId: this.formInline.categoryId
        },
        pageCount: 10,
        page: this.page
      };
       if(this.formInline.categoryId.length == 0){
          delete data.searchCriteria.categoryId;
        }else{
          data.searchCriteria.categoryId = this.formInline.categoryId[this.formInline.categoryId.length-1];
        }
      listTenant(this.formInline.gongsiSel,data).then((res)=>{
        console.log(res)
        this.dataList = res.result.goods;
        this.total = res.result.totalCount;
      })
    },    
    //table选择的商品
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    
    //删除商品
    confirmDele(){
      this.$confirm('确认删除?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          var arr = [];
          for(let i = 0;i<this.multipleSelection.length;i++){
            arr.push(this.multipleSelection[i].id)
          }
          var list = {
            "tenantId": this.formInline.gongsiSel,
            "goodsIds": arr
          };
          offsaleMaintain(list).then((res)=>{
            // this.dialogFillBuckle = false;
            if(res.result.fails && res.result.fails.length>0){
              this.$message({
                type: 'error',
                message: res.result.fails[0].reason
              })
            }else{
              this.initData();
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            }
          })
        })
        .catch(err => { console.error(err) })
    },
    //获取商品类
    orderListData(){
      category().then((res)=>{
        console.log(res)
        this.orderList = res.result;
      })
    },
    openDeleteGood(){
      this.dialogFillBuckle = true;
      this.productType = 'del';
    },
    //导出
    handleExport(){

    },
    //重置
    handleReset(){
      this.formInline = {
        name: '',
        categoryId: '',
        gongsiSel: this.gongsiList[0].id
      };
      this.initData();
    },
    //过滤查询
    check(){
      this.initData();
    },
    //分页
    pagination(data) {
      console.log(data)
      this.page = data.page-1;
      this.initData();
    },
    handleAddRole() {
      this.role = Object.assign({}, defaultRole)
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedNodes([])
      }
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true;
      this.goodId = scope.row.id;
      this.role.normal.name = scope.row.name;
      this.role.normal.topCategoryId = scope.row.topCategoryId;
      this.role.normal.secondCategoryId = scope.row.secondCategoryId;
      this.role.normal.img = scope.row.img;
      this.role.normal.price = scope.row.price;
      this.role.normal.cost = scope.row.cost;
    },
    async confirmRole() {
      var list = {
          "price": Number(this.role.normal.price),
          "cost": Number(this.role.normal.price),
          "discount": Number(this.role.normal.discount)
      };
      putTenant(this.goodId,list).then((res)=>{
        this.dialogVisible = false;
        this.initData();
        this.$message({
          type: 'success',
          message: '编辑成功!'
        })
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
